// 完整的78张塔罗牌数据
const tarotCardsArray = [
	{
		"id": 1,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar01.jpg"
	},
	{
		"id": 2,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar02.jpg"
	},
	{
		"id": 3,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar03.jpg"
	},
	{
		"id": 4,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar04.jpg"
	},
	{
		"id": 5,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar05.jpg"
	},
	{
		"id": 6,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar06.jpg"
	},
	{
		"id": 7,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar07.jpg"
	},
	{
		"id": 8,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar08.jpg"
	},
	{
		"id": 9,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar09.jpg"
	},
	{
		"id": 10,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar10.jpg"
	},
	{
		"id": 11,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar11.jpg"
	},
	{
		"id": 12,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar12.jpg"
	},
	{
		"id": 13,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar13.jpg"
	},
	{
		"id": 14,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar14.jpg"
	},
	{
		"id": 15,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar15.jpg"
	},
	{
		"id": 16,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar16.jpg"
	},
	{
		"id": 17,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar17.jpg"
	},
	{
		"id": 18,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar18.jpg"
	},
	{
		"id": 19,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar19.jpg"
	},
	{
		"id": 20,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar20.jpg"
	},
	{
		"id": 21,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar00.jpg"
	},
	{
		"id": 22,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/ar21.jpg"
	},
	{
		"id": 23,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/wapa.jpg"
	},
	{
		"id": 24,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/wakn.jpg"
	},
	{
		"id": 25,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/waqu.jpg"
	},
	{
		"id": 26,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/waki.jpg"
	},
	{
		"id": 27,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/waac.jpg"
	},
	{
		"id": 28,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/wa02.jpg"
	},
	{
		"id": 29,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/wa03.jpg"
	},
	{
		"id": 30,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/wa04.jpg"
	},
	{
		"id": 31,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/wa05.jpg"
	},
	{
		"id": 32,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/wa06.jpg"
	},
	{
		"id": 33,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/wa07.jpg"
	},
	{
		"id": 34,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/wa08.jpg"
	},
	{
		"id": 35,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/wa09.jpg"
	},
	{
		"id": 36,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/wa10.jpg"
	},
	{
		"id": 37,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/cupa.jpg"
	},
	{
		"id": 38,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/cukn.jpg"
	},
	{
		"id": 39,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/cuqu.jpg"
	},
	{
		"id": 40,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/cuki.jpg"
	},
	{
		"id": 41,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/cuac.jpg"
	},
	{
		"id": 42,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/cu02.jpg"
	},
	{
		"id": 43,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/cu03.jpg"
	},
	{
		"id": 44,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/cu04.jpg"
	},
	{
		"id": 45,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/cu05.jpg"
	},
	{
		"id": 46,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/cu06.jpg"
	},
	{
		"id": 47,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/cu07.jpg"
	},
	{
		"id": 48,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/cu08.jpg"
	},
	{
		"id": 49,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/cu09.jpg"
	},
	{
		"id": 50,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/cu10.jpg"
	},
	{
		"id": 51,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/pepa.jpg"
	},
	{
		"id": 52,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/pekn.jpg"
	},
	{
		"id": 53,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/pequ.jpg"
	},
	{
		"id": 54,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/peki.jpg"
	},
	{
		"id": 55,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/peac.jpg"
	},
	{
		"id": 56,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/pe02.jpg"
	},
	{
		"id": 57,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/pe03.jpg"
	},
	{
		"id": 58,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/pe04.jpg"
	},
	{
		"id": 59,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/pe05.jpg"
	},
	{
		"id": 60,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/pe06.jpg"
	},
	{
		"id": 61,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/pe07.jpg"
	},
	{
		"id": 62,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/pe08.jpg"
	},
	{
		"id": 63,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/pe09.jpg"
	},
	{
		"id": 64,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/pe10.jpg"
	},
	{
		"id": 65,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/swpa.jpg"
	},
	{
		"id": 66,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/swkn.jpg"
	},
	{
		"id": 67,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/swqu.jpg"
	},
	{
		"id": 68,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/swki.jpg"
	},
	{
		"id": 69,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/swac.jpg"
	},
	{
		"id": 70,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/sw02.jpg"
	},
	{
		"id": 71,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/sw03.jpg"
	},
	{
		"id": 72,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/sw04.jpg"
	},
	{
		"id": 73,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/sw05.jpg"
	},
	{
		"id": 74,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/sw06.jpg"
	},
	{
		"id": 75,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/sw07.jpg"
	},
	{
		"id": 76,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/sw08.jpg"
	},
	{
		"id": 77,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/sw09.jpg"
	},
	{
		"id": 78,
		"image_url": "https://sacred-texts.com/tarot/pkt/img/sw10.jpg"
	}
];

// 导入API配置
import { buildApiUrl, TAROT_API } from '@/config/api.js';

// 数据缓存
let cachedData = null;
let cacheTimestamp = 0;
const CACHE_DURATION = 5 * 60 * 1000; // 缓存5分钟

/**
 * 从API获取塔罗牌数据
 * @returns {Promise<Object>} 返回API响应数据
 */
export const fetchTarotCards = async () => {
	// 检查缓存是否有效
	const now = Date.now();
	if (cachedData && (now - cacheTimestamp) < CACHE_DURATION) {
		console.log('使用缓存的塔罗牌数据');
		return cachedData;
	}
	
	try {
		const url = buildApiUrl(TAROT_API.getList);
		console.log('正在从API获取塔罗牌数据:', url);
		
		const response = await uni.request({
			url: url,
			method: 'GET',
			timeout: 10000 // 10秒超时
		});
		
		// 检查HTTP状态
		if (response.statusCode !== 200) {
			throw new Error(`HTTP错误: ${response.statusCode}`);
		}
		
		const data = response.data;
		
		// 检查业务状态码
		if (data.code !== 1) {
			throw new Error(data.msg || '获取数据失败');
		}
		
		// 验证数据结构
		if (!data.data || !Array.isArray(data.data.list)) {
			throw new Error('数据格式错误');
		}
		
		// 更新缓存
		cachedData = data;
		cacheTimestamp = now;
		
		console.log('成功获取塔罗牌数据，共', data.data.list.length, '张');
		return data;
		
	} catch (error) {
		console.error('获取塔罗牌数据失败:', error);
		
		// 返回默认数据作为降级方案
		console.log('使用默认塔罗牌数据');
		return {
			code: 1,
			msg: "使用默认数据",
			time: String(Math.floor(Date.now() / 1000)),
			data: {
				list: tarotCardsArray
			}
		};
	}
};

/**
 * 清除缓存
 */
export const clearTarotCache = () => {
	cachedData = null;
	cacheTimestamp = 0;
	console.log('塔罗牌数据缓存已清除');
};

// 模拟API接口返回的数据结构（用于静态导入）
export const tarotCardsResponse = {
	code: 1,
	msg: "获取成功",
	time: "1763374156",
	data: {
		list: tarotCardsArray
	}
};

// 为了保持向后兼容，也导出原始数组
export const tarotCards = tarotCardsArray;
